<?php

class cometModel extends Model
{
	private $tb='comet';
	
	//获取用户所有未读信息条数
	/*
	参数：uid
	返回格式
	2维数组：[i][fromid=>'' 未读消息数=>''];
	*/
	public function count($uid)
	{
		$sql="select fromid,count(*) from $this->tb where toid=$uid and isread=0 group by fromid";
		return $this->db->getAll($sql);
	}

	//获取用户所有未读信息
	/*
	参数：uid
	返回格式
	2维数组：[i][fromid=>'' content=>'' time=>''];
	*/
	public function allnotread($uid)
	{
		$sql="select * from $this->tb where toid=$uid and isread=0 order by id";
		$rs=$this->db->getAll($sql);
		if($rs)
		{
			/*foreach($rs as $v)
			{
				$this->hasread($v['id']);
			}*/
			$arr=array();
			foreach($rs as $v)
			{
				$arr[]=($v['id']);
			}
			$this->setread($arr);
		}
		else
		{
			return false;
		}
		return $rs;
	}

	public function sort_comet($cmarray)
	{
		$list=array();
		if(!count($cmarray)||!$cmarray)
		{
			return false;
		}

		foreach($cmarray as $cv)
		{
			foreach($list as $lk=>$lv)
			{
				if($lv['msgs'][0]['fromid']==$cv['fromid'])
				{
					$list[$lk]['msgs'][]=$cv;
				}
				else
				{
					$list[]['msgs'][]=$cv;
				}
			}
		}
		return $list;
	}

	//将单条消息设为已读
	public function hasread($id)
	{
		$this->db->lock($this->tb);
		$sql="update $this->tb set isread=1 where id=$id limit 1";
		if($this->db->set_query($sql))
		{
			$this->db->unlock();
			return $this->db->affect_row();
		}
		else
		{
			return false;
		}
	}
	//读取一条消息
	public function readone($uid)
	{
		$this->db->lock($this->tb);
		$sql="select * from $this->tb where isread=0 and toid=$uid limit 1";
		$cm=$this->db->getRow($sql);
		$this->db->unlock();
		return $cm;

	}
	//添加一条消息
	public function add($data)
	{
		/*
		//$this->db->lock($this->tb);
		if($this->db->insert($this->tb,$data))
		{
			//$this->db->unlock();
			return $this->db->insert_id();
		}
		else
		{
			//$this->db->unlock();
			return false;
		}
		*/
		$this->db->lock($this->tb);
		if($this->db->insert($this->tb,$data))
		{
			$cmid=$this->db->insert_id();
		}
		$this->db->unlock();
		return $cmid;


	}

	//获取所有用户的所有未读消息2015/7/14
	/*
	参数
	array(
		用户id,
	);

	*/
	public function read($u_arr)
	{
		$this->db->lock($this->tb);
		$sql="select * from $this->tb where isread=0 and toid in(".implode(",",$u_arr).") limit 10";
		//var_dump($sql);
		$all=$this->db->getAll($sql);
		$this->db->unlock();
		return $all;
	}

	//批量设置已读消息2015/7/14
	/*
	参数 
	array(
		消息id
	);
	将数组内所有消息id都设为已读
	*/
	public function setread($arr)
	{	
		$this->db->lock($this->tb);
		//参考http://www.jb51.net/article/41852.htm
		$sql="replace into $this->tb (id,isread) values ";
		foreach($arr as $v)
		{
			$sql.="($v,1),";
		}
		$sql=rtrim($sql,',');
		//echo $sql;
		//var_dump($sql);
		$set=$this->db->set_query($sql);
		$this->db->unlock();
		return $set;
	}



	//获取未读消息数
	public function getnum($uid,$fid)
	{
		$sql="select count(*) from $this->tb where isread=0 and toid=$uid and fromid=$fid";
		return $this->db->getOne($sql);
	}

	//获取好友的最后一条消息,不设已读
	public function getlast($uid,$fid)
	{
		$sql="select * from $this->tb where (toid=$uid and fromid=$fid) or (toid=$fid and fromid=$uid) order by time desc limit 1";
		return $this->db->getRow($sql);
	}

	//获取最多60条双方信息
	public function getboth($uid,$fid,$offset=0,$last=-1,$need=60)
	{
		//$last=-1 初始加载/最后一页
		$litype='desc';
		$lg='>';
		if($last==0)//上页
		{
			$litype='desc';
			$lg='<';
		}
		else if($last==1)//下页/第一页
		{
			$litype='asc';
			$lg='>';
		}
		$more=$offset<=0?'':' and id'.$lg.$offset;
	
		$sql="select * from $this->tb where ((toid=$uid and fromid=$fid) or (toid=$fid and fromid=$uid)) $more order by time $litype limit $need";
	
		return $this->db->getAll($sql);
	}
	//获取id之间60条信息
	public function getbetween($uid,$fid,$middleid,$btweennum=60)
	{
		$btweennum-=4;
		$sql="select * from $this->tb where ((toid=$uid and fromid=$fid) or (toid=$fid and fromid=$uid)) and id<=$middleid order by time desc limit 4";
		$head=$this->db->getAll($sql);
		$sql="select * from $this->tb where ((toid=$uid and fromid=$fid) or (toid=$fid and fromid=$uid)) and id>$middleid order by time limit $btweennum";
		$tail=$this->db->getAll($sql);
		if(!empty($head)&&!empty($tail))
		{
			sort($head);
			return array_merge($head,$tail);
		}
		else if(empty($head)&&!empty($tail))
		{
			return $tail;
		}
		else if(!empty($head)&&empty($tail))
		{
			sort($head);
			return $head;
		}
		else
		{
			return false;
		}
	}


	//计算偏移值范围的消息数
	public function getoffsetcount($uid,$fid,$offset=0,$last=-1,$need=60)
	{	
		$litype='desc';
		$lg='>';
		if($last==0)
		{
			$litype='desc';
			$lg='<';
		}
		else if($last==1)
		{
			$litype='asc';
			$lg='>';
		}
		$more=' and id'.$lg.$offset;
		$sql="select count(*) from (select id from $this->tb where (toid=$uid and fromid=$fid $more) or (toid=$fid and fromid=$uid $more) order by time $litype limit $need) as countlist";
		
		return $this->db->getOne($sql);
	}
	/*
	public function getoffsetcount($uid,$fid,$more='',$need=60)
	{
		$more=$more==''?'':'and '.$more;
		$sql="select count(*) from (select id from $this->tb where (toid=$uid and fromid=$fid $more) or (toid=$fid and fromid=$uid $more) order by time desc limit $need) as countlist";
		return $this->db->getOne($sql);
	}
	*/


	public function search($uid,$fid,$key,$range=0)
	{
		$rg=$range==0?'':' and time>'.strtotime('-'.$range.' days');
		$sql="select * from $this->tb where ((toid=$uid and fromid=$fid) or (toid=$fid and fromid=$uid)) and content like '%$key%' and mode='text'$rg order by id desc";
		//var_dump($sql);
		return $this->db->getAll($sql);
	}

	//根据给定的时间戳来获取其月内的所有消息的日期
	public function getdate($uid,$fid,$timstamp)
	{
		$sql="select distinct FROM_UNIXTIME(time,'%e') as day from $this->tb where ((toid=$uid and fromid=$fid) or (toid=$fid and fromid=$uid)) and FROM_UNIXTIME(time,'%Y%m') = FROM_UNIXTIME($timstamp ,'%Y%m')";
		return $this->db->getAll($sql);
	}

	public function getbydate($uid,$fid,$timestamp)
	{
		$sql="select * from $this->tb where ((toid=$uid and fromid=$fid) or (toid=$fid and fromid=$uid)) and time>=$timestamp order by time limit 60";
		return $this->db->getAll($sql);
	}
}